package com.njupt.towerssystem.controller;

import com.njupt.towerssystem.domain.ManagerInfos;
import com.njupt.towerssystem.services.ManageService;
import com.njupt.towerssystem.services.PowerService;
import com.njupt.towerssystem.services.RegisterUserService;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("/sys")
@CrossOrigin("*")
public class ManagerInfoCtrl {

    @Resource
    private ManageService manageService;

    @Resource
    private RegisterUserService registerUserService;

    @Resource
    private PowerService powerService;

    // 用户注册
    @RequestMapping("userreg")
    public String reg(ManagerInfos mi) {
        boolean ret = registerUserService.regUser(mi);
        if (ret) {
            return "{\"userreg\":\"success\"}";
        } else {
            return "{\"userreg\":\"fail\"}";
        }
    }
    // 用户登录
    @RequestMapping("/login")
    public ManagerInfos login(
            @RequestParam(required = false) String loginname,
//        @RequestParam(required = false, name = "loginname") String loginname;
// false代表不是必须 name可以不写，但是前后端名要相同
            @RequestParam(required = false) String loginpwd,
            @RequestParam(required = false) String token
    ) {
        return manageService.login(loginname,loginpwd,token);
    }

    @RequestMapping("/userpower")
    public String setuserpower(@RequestParam(required = false) int ruid
            ,int userid, int ruleid, String iseffective) {
        if (iseffective.equalsIgnoreCase("y")) {
            powerService.grantUserRule(ruid, ruleid, userid);
        } else {
            powerService.revokeUserRule(ruleid,userid);
        }
        return "{\"powerset\": \"success\"}";
    }

    @RequestMapping("/insertPower")
    public String regpower(int ruleid, String rulename) {
        powerService.addNewRule(ruleid,rulename);
        return "{\"powerset\": \"success\"}";
    }
}
